package com.yingside.io.bookio;

import java.util.List;
import java.util.Scanner;

public class BookShow {
    private Scanner input = new Scanner(System.in);
    private IBookManager bookManager =  new BookManagerImpl();
    public void menu(){
        while(true){
            System.out.println("图书管理");
            System.out.println("1、图书显示");
            System.out.println("2、图书查询");
            System.out.println("3、图书新增");
            System.out.println("4、图书删除");
            System.out.println("5、图书修改");
            System.out.println("6、退出程序");
            System.out.println("请选择(1-6)");
            int m = input.nextInt();

            switch (m){
                case 1:
                    List<Book> list = bookManager.list();
                    show(list);
                    break;
                case 2:
                    System.out.println("请输入你要查询图书的名字");
                    String bookName = input.next();
                    Book book = bookManager.get(bookName);
                    showBook(book);
                    break;
                case 3:
                    addBook();
                    break;
                case 5:
                    updateBook();
                    break;
                case 6:
                    System.exit(0);
                    break;
            }
        }

    }

    public Book inputBook(){
        System.out.println("请输入图书序号:");
        int id = input.nextInt();
        System.out.println("请输入图书名称:");
        String bookName = input.next();
        System.out.println("请输入图书作者:");
        String author = input.next();
        System.out.println("请输入图书价格:");
        int price = input.nextInt();

        Book book = new Book(id,bookName,author,price);
        return book;
    }

    private void updateBook() {
        Book book = inputBook();

        Book b = bookManager.get(book.getId());

        if(b == null){
            System.out.println("没有你查找的数据");
        }
        else{
            boolean flag = bookManager.update(book);

            if(flag){
                System.out.println("修改成功");
            }
            else{
                System.out.println("修改失败");
            }
        }
    }

    public boolean addBook(){

        Book book = inputBook();

        boolean flag = bookManager.add(book);

        return flag;
    }

    private void showBook(Book book){
        if(book == null){
            System.out.println("没有这本数据");
        }
        else{
            System.out.println("图书编号\t图书名称\t图书作者\t图书价格\t");
            System.out.println(book.getId() + "\t"
                    + book.getBookName() + "\t"
                    + book.getAuthor() + "\t"
                    + book.getPrice());
        }
    }

    private void show(List<Book> list){
        System.out.println("图书编号\t图书名称\t图书作者\t图书价格\t");
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            System.out.println(book.getId() + "\t"
                    + book.getBookName() + "\t"
                    + book.getAuthor() + "\t"
                    + book.getPrice());
        }
    }
}
